<?php
namespace Admin\Controller;
class ArticleController extends AdminController {	
/*
 *新闻部分的增删改操作
 */
#文章列表

public function index() {

	$cate = M('Category')->getField('id,name');
	$path = I('get.path');
	$key  = I('get.key');

	$article = M('Article');

	if($path!=''){
		$map['path'] = array('like', $path.'%');
	}

	if($key!=''){
		$map['title'] = array('like', '%'.$key.'%');
	}

	$count = $article->where($map)->count();

	import("Common.ORG.Page");
	$page = new \Page($count, 20);
	$show = $page->show();

	$list = $article->where($map)->order('sort asc , id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

	$artClass = M('Category');
	$sclass = $artClass->field("id,name,fid,path")->order('path')->select();

	foreach ($sclass as $key => $value) {
		$count = count(explode('-', $value['path'])) - 3;
		$sclass[$key]['count'] = $count;
	}

	$this->assign('sclass', $sclass);
	$this->assign('list', $list);
	$this->assign('cate', $cate);
	$this->assign('page', $show);
	$this->assign('path', $path);

	$this->display();
}	


#文章添加
public	function Add() {
	$path = $_GET['path'];
	$artClass = M('Category');
	$list = $artClass->field("id,name,fid,path")->order('path')->select();		
	foreach ($list as $key => $value) {
		$count = count(explode('-', $value['path'])) - 3;
		$list[$key]['count'] = $count;
	}

	$this->assign('list', $list);
	$this->assign('path', $path);
	$this->display();
}


#文章保存

public function Save() {	
	$article = D('Article');
	if ($date = $article->create()) {
		if ($article->add($date)) {
			$state = 'SUCCESS';
            $message = 'Article add success';
            $url = U('Article/index');
		} else {
			$state = 'Add failed';
		}
	} else {
		$state = $article->getError();
	}

	echo $this->echo_json_str($state,$message,$url);
}



	#文章编辑

public function Edit() {

	$id = (int) $_GET['id'];
		if (!isset ($id)) {
			$this->error('Parameter error');
		}

		$artClass = M('Category');
		$clist = $artClass->field("id,name,fid,path")->order('path')->select();
		foreach ($clist as $key => $value) {
			$count = count(explode('-', $value['path'])) - 3;
			$clist[$key]['count'] = $count;
		}

		$article = M('Article');
		$list = $article->where("id=$id")->find();
		if (!$list) {
			$this->error(L('_public_noinfo'));
		} else {
			if ($list['picname']=='') {
				$list['pic'] = STATICS.'/image/image.jpg';
			}else{
				$list['pic'] = $list['picname'];
			}
			$this->assign('list', $list);
			$this->assign('clist', $clist);
			$this->display();
		}
}


#更新文章

public function Update() {

	$article = D('Article');
	if ($date = $article->create()) {
		if ($article->save($date)) {
			$state = 'SUCCESS';
            $message = 'Article editor success';
            $url = U('Article/index');
		} else {
			$state = 'Editor failed';
		}
	} else {
		$state = $article->getError();
	}
	echo $this->echo_json_str($state,$message,$url);
}



#文章删除
public function Del() {

	$id=I('post.selectedids');
	if($id==''){
		$state = "You do not select any information!";
	}else{
		foreach ($id as $v) {
			$article = M('Article');
			$where['id'] = $v;
			$list = $article->field('picname')->where($where)->find();
			if ($list['picname']!='') {
				unlink(C('SITE_PATH').$list['picname']);
			}
			$article->where($where)->delete();
		}

		$state = 'SUCCESS';
        $message = 'Delete success';
        $url = U('Article/index');
	}
	echo $this->echo_json_str($state,$message,$url);
}

}
?>